package com.itany.hotel.service.impl;

import com.itany.hotel.dao.MemberMapper;
import com.itany.hotel.entity.Member;
import com.itany.hotel.exception.DuplicateUsernameException;
import com.itany.hotel.exception.UserNotFoundException;
import com.itany.hotel.service.MemberService;
import framework.bean.factory.ObjectFactory;

import java.util.Date;


public class MemberServiceImpl implements MemberService {
    @Override
    public void regist(Member member) throws DuplicateUsernameException {
        MemberMapper memberDao= (MemberMapper) ObjectFactory.getObject("memberMapper");


        Member m=memberDao.selectByUsername(member.getUsername());

        if(m!=null){
            throw new DuplicateUsernameException("用户名已存在");
        }

        member.setRegtime(new Date());

        memberDao.insert(member);

    }

    @Override
    public Member login(String username, String password) throws UserNotFoundException {
        MemberMapper memberDao= (MemberMapper) ObjectFactory.getObject("memberMapper");

        Member member=memberDao.selectByUsernameAndPassword(username,password);

        if(member==null){
            throw new UserNotFoundException("用户名或密码错误");
        }

        return member;
    }
}
